package com.lz.sports.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lz.library.adapter.BindingViewHolder
import com.lz.library.extension.APP
import com.lz.sports.bean.PageList
import com.lz.sports.databinding.ItemFooterBinding

class FooterAdapter : RecyclerView.Adapter<BindingViewHolder>() {

    private var state = PageList.EMPTY

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {
        val binding = ItemFooterBinding.inflate(LayoutInflater.from(APP), parent, false)
        return BindingViewHolder(binding.root)
    }

    override fun onBindViewHolder(vh: BindingViewHolder, position: Int) {
        vh.getBinding<ItemFooterBinding>().state = state
    }

    override fun getItemCount(): Int = if (state == PageList.EMPTY) 0 else 1

    fun setFooterState(newState: Int) {
        if (this.state == newState) return
        val needNotifyAll = this.state == PageList.EMPTY || newState == PageList.EMPTY
        this.state = newState
        if (needNotifyAll) notifyDataSetChanged() else notifyItemChanged(0)
    }
}